package com.devops.admin.graphql;

import com.devops.admin.model.KafkaTopic;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSBindStatusProject;
import com.devops.admin.po.KafkaTopicPo;
import com.devops.admin.service.KafkaTopicService;
import com.devops.admin.vo.DOSPageVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.devops.admin.vo.KafkaTopicVo;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jxt
 * @Notes
 * @date 2023/5/17 17:47
 */
@Slf4j
@DgsComponent
public class DOSKafkaTopicDataFetcher {

    @Resource
    private KafkaTopicService kafkaTopicService;

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public TableResult<KafkaTopicVo> getKafkaTopicList(@InputArgument KafkaTopicPo kafkaTopicPo, @InputArgument DOSPageVo pageVo){
        return kafkaTopicService.getKafkaTopicList(kafkaTopicPo, pageVo);
    }

    @DgsMutation
    @DOSLog(description = "创建kafka的topic")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR},logical = Logical.OR)
    public Boolean createKafkaTopic(@InputArgument KafkaTopicPo kafkaTopicPo){
        return kafkaTopicService.createKafkaTopic(kafkaTopicPo);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public List<KafkaTopicVo> getKafkaTopicsByProject(@InputArgument Integer projectId){
        return kafkaTopicService.getKafkaTopicsByProject(projectId);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public KafkaTopicVo getKafkaTopicById(@InputArgument Integer topicId){
        return kafkaTopicService.getKafkaTopicById(topicId);
    }

    @DgsMutation
    @DOSLog(description = "kafka的topic与项目绑定")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR},logical = Logical.OR)
    public Boolean relateKafkaTopicToProjects(@InputArgument Integer topicId, @InputArgument List<Integer> projectIds){
        return kafkaTopicService.relateKafkaTopicToProjects(topicId, projectIds);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public List<DOSBindStatusProject> getProjectsBindStatusByKafkaTopicId(@InputArgument Integer topicId){
        return kafkaTopicService.getProjectsBindStatusByKafkaTopicId(topicId);
    }

    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR},logical = Logical.OR)
    public Boolean refreshKafkaTopicList(){
        kafkaTopicService.syncKafkaTopic();
        return true;
    }

    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR},logical = Logical.OR)
    public Boolean updateKafkaTopicDescription(@InputArgument Integer topicId, @InputArgument String description){
        return kafkaTopicService.updateKafkaTopicDescription(topicId, description);
    }
}
